//
//  NetDataSource.m
//  Weikan
//
//  Created by maming on 11-8-10.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "NetDataSource.h"
#import "NSObject+SBJSON.h"
#import "DataSource.h"
@implementation NetDataSource

+ (NetDataSource*)shareInstance
{
	static NetDataSource * instance = nil;
	if (instance == nil)
	{
		instance = [[NetDataSource alloc]init];
	}
	return instance;
}

- (void)postTo:(NSString*)url postInfo:(NSDictionary*)info userInfo:(CoreMsg*)msg
{
//	if (info == nil || [info count] <= 0) {
//		return;
//	}
    
	NSURL * weikanUrl = nil;
	if (url == nil) {
//		weikanUrl = [NSURL URLWithString:WEIKAN_WEBADDR];
	} else {
		weikanUrl = [NSURL URLWithString:url];
	}
    
	ASIFormDataRequest * request = [[ASIFormDataRequest alloc]initWithURL:weikanUrl];
    [request setRequestMethod:@"POST"];
    
	[request setDelegate:self];
	request.timeOutSeconds = HTTP_POST_TIMEOUT;
	
	NSDictionary *userInfo = [[NSDictionary alloc] initWithObjectsAndKeys: msg, @"userinfo", nil];
	request.userInfo = userInfo;
    if (info) {
        NSArray *arrKeys=[info allKeys];
        for (int i = 0; i < [arrKeys count]; i++) {
            if ([[arrKeys objectAtIndex:i] isEqualToString:@"cover_image"]) {
                [request addFile:[info valueForKey:[arrKeys objectAtIndex:i]]  forKey:@"cover_image"];
            }
            else
                [request setPostValue:[info valueForKey:[arrKeys objectAtIndex:i]] forKey:[arrKeys objectAtIndex:i]];
        }
    }
	[request startAsynchronous];
}


- (void)postFileTo:(NSString*)url postInfo:(NSString*)path userInfo:(CoreMsg*)msg
{
    //	if (info == nil || [info count] <= 0) {
    //		return;
    //	}
    
	NSURL * weikanUrl = nil;
	if (url == nil) {
        //		weikanUrl = [NSURL URLWithString:WEIKAN_WEBADDR];
	} else {
		weikanUrl = [NSURL URLWithString:url];
	}
    
	ASIFormDataRequest * request = [[ASIFormDataRequest alloc]initWithURL:weikanUrl];
    [request setRequestMethod:@"POST"];

	[request setDelegate:self];
	request.timeOutSeconds = HTTP_POST_TIMEOUT;
	
	NSDictionary *userInfo = [[NSDictionary alloc] initWithObjectsAndKeys: msg, @"userinfo", nil];
	request.userInfo = userInfo;
    if (path) {
        [request addFile:path forKey:@"personal_image"];
    }
	[request startAsynchronous];
}

- (void)getFrom:(NSString*)url getInfo:(NSDictionary*)info userInfo:(CoreMsg*)msg
{
//	NSString * fullUrl = url;
//    if (info && [info count] > 0) 
//	{
//        NSArray * allKeys = [info allKeys];
//        fullUrl = [url stringByAppendingString:[[NSString stringWithFormat:@"?%@=%@", [allKeys objectAtIndex:0], [info valueForKey:[allKeys objectAtIndex:0]]] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
//        for (int i = 1; i < [info count]; i++) {
//            fullUrl = [fullUrl stringByAppendingString:[[NSString stringWithFormat:@"&%@=%@", [allKeys objectAtIndex:i], [info valueForKey:[allKeys objectAtIndex:i]]] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
//        }
//    }
	NSURL * weikanUrl = [NSURL URLWithString:url];
        
    ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:weikanUrl];
    
//	[request setRequestMethod:@"GET"];
	[request setDelegate:self];
    [request setCachePolicy:ASIDoNotWriteToCacheCachePolicy];
    request.timeOutSeconds = HTTP_POST_TIMEOUT;
	
	NSDictionary *userInfo = [[NSDictionary alloc]initWithObjectsAndKeys: msg, @"userinfo", nil];
	request.userInfo = userInfo;
    if (info) {
        NSArray *arrKeys=[info allKeys];
        for (int i = 0; i < [arrKeys count]; i++) {
            [request addRequestHeader:[arrKeys objectAtIndex:i] value:[info valueForKey:[arrKeys objectAtIndex:i]]];
        }
    }
	
	[request startAsynchronous];
}

- (NSString*)getErrorString:(NSString*)errorString
{
    return [errorString substringFromIndex:[errorString rangeOfString:@"ERROR"].location + 6];
}

- (void)requestFinished:(ASIHTTPRequest*)request
{
    NSString* responseString = [request responseString];
//    NSLog(@"response %@",responseString);
    id responseData = [responseString JSONValue];
	CoreMsg * msg = [request.userInfo objectForKey:@"userinfo"];
    if (msg.ui_delegate == nil)
    {
        return;
    }
    if ([responseString rangeOfString:@"ERROR"].length > 1) {
        if ([msg.ui_delegate respondsToSelector:@selector(onDataSourceFailCallback:withData:)]) {    
            [msg.ui_delegate onDataSourceFailCallback:msg withData:[self getErrorString:responseString]];
        }
    }
    else if ([msg.ui_delegate respondsToSelector:@selector(onDataSourceSucCallback:withData:)]) {
        if ([msg.data isEqual: LOGININFO]) {
            [msg.ui_delegate onDataSourceSucCallback:msg withData:nil];
        }
        else if ([msg.data isEqual: SIGNUP])
        {
            [msg.ui_delegate onDataSourceSucCallback:msg withData:nil];
        }
        else if ([msg.data isEqual: FINDOUT])
        {
            [msg.ui_delegate onDataSourceSucCallback:msg withData:nil];
        }
        else if ([msg.data isEqual: PERSONINFO])
        {
            [msg.ui_delegate onDataSourceSucCallback:msg withData:[self userinfoParser:responseData]];
        }
        else if ([msg.data isEqual: RECOMMENDINFO])
        {
            [msg.ui_delegate onDataSourceSucCallback:msg withData:[self recommendinfoParser:responseData]];
        }
        else if ([msg.data isEqual: CREATTRIP])
        {
            [msg.ui_delegate onDataSourceSucCallback:msg withData:[self creattripinfoParser:responseData]];
        }
        else if ([msg.data isEqual: CITYLIST])
        {
            [msg.ui_delegate onDataSourceSucCallback:msg withData:[self newCitylistinfoParser:responseData]];
        }
        else if ([msg.data isEqual: ELEMENTSEARCH])
        {
            [msg.ui_delegate onDataSourceSucCallback:msg withData:[self elementinfoParser:responseData]];
        }
        else if ([msg.data isEqual: TRAVELDETAIL])
        {
            [msg.ui_delegate onDataSourceSucCallback:msg withData:[self tripinfoParser:responseData]];
        }
        else if ([msg.data isEqual: CREATCOMMENT])
        {
            [msg.ui_delegate onDataSourceSucCallback:msg withData:[self tripinfoParser:responseData]];
        }
        else if ([msg.data isEqual: MYTRIP])
        {
            [msg.ui_delegate onDataSourceSucCallback:msg withData:[self mytripinfoParser:responseData]];
        }
        else if ([msg.data isEqual: COLLECTTRIP])
        {
            [msg.ui_delegate onDataSourceSucCallback:msg withData:[self mytripinfoParser:responseData]];
        }
        else if ([msg.data isEqual: MYCOMMENT])
        {
            [msg.ui_delegate onDataSourceSucCallback:msg withData:[self mycommentinfoParser:responseData]];
        }
        else if ([msg.data isEqual: FEEDBACK])
        {
            [msg.ui_delegate onDataSourceSucCallback:msg withData:nil];
        }
        else if ([msg.data isEqual: SECRETMODIFY])
        {
            [msg.ui_delegate onDataSourceSucCallback:msg withData:nil];
        }
        else if ([msg.data isEqual: SHOPDETAIL])
        {
            [msg.ui_delegate onDataSourceSucCallback:msg withData:[self placeDetailinfoParser:responseData]];
        }
        else if ([msg.data isEqual: PERSONIMAGE])
        {
            [msg.ui_delegate onDataSourceSucCallback:msg withData:nil];
        }
        else if ([msg.data isEqual: CREATEVENTLIST]){
            [msg.ui_delegate onDataSourceSucCallback:msg withData:[self intertEventListParser:responseData]];
        }
        else if ([msg.data isEqual: COMMENTLIST])
        {
            [msg.ui_delegate onDataSourceSucCallback:msg withData:[self commentinfoListParser:responseData]];
        }
        else if ([msg.data isEqual: COMMENTTRAVEL])
        {
            [msg.ui_delegate onDataSourceSucCallback:msg withData:nil];
        }
        else if ([msg.data isEqual: UPDATEPERSONINFO])
        {
            [msg.ui_delegate onDataSourceSucCallback:msg withData:nil];
        }
        else if ([msg.data isEqual: TRAVELEDITLINE])
        {
            [msg.ui_delegate onDataSourceSucCallback:msg withData:nil];
        }
        else if ([msg.data isEqual: DELEDITEVENTLIST])
        {
            [msg.ui_delegate onDataSourceSucCallback:msg withData:nil];
        }
        else if ([msg.data isEqual: RECOMMENDMAPEVENT])
        {
            [msg.ui_delegate onDataSourceSucCallback:msg withData:[self recommendmapeventParser:responseData]];
        }
    }
	[request release];
}   

- (void)requestFailed:(ASIHTTPRequest*)request
{
	NSDictionary *userInfo = request.userInfo;
    CoreMsg * msg = [userInfo objectForKey:@"userinfo"];
    if ([msg.ui_delegate respondsToSelector:@selector(onDataSourceSucCallback:withData:)]) {
        [msg.ui_delegate onDataSourceFailCallback:msg withData:nil];
    }    
	[request release];
}

@end


